package com.tanhua.app.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.tanhua.app.service.CommentService;
import com.tanhua.app.service.MovementService;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
    private MovementService movementService;

    @Autowired
    private CommentService commentService;

    /**
     * 发布动态
     * @return
     */
    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile imageContent[]) throws IOException {

        //1.调用Servicec执行业务逻辑
        movementService.publishMovement(movement, imageContent);

        //2.返回结果
        return ResponseEntity.ok(null);
    }


    /**
     * 查询我的动态（根据登录的用户id查询，并分页查询）
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity all(Long userId, @RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10") Integer pagesize){
        //前端页面需要展示的数据包含了两个属性类:userInfo和movement，所以在返回前端需要的数据内容是时，需要创建一个vo对象来封装数据
        //1.调用Service执行业务逻辑
        PageResult<MovementsVo> pageResult = movementService.findByUserIdMovement(userId,page,pagesize);

        //2.返回结果
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 查询好友动态（分页展示）
     * @return
     */
    @GetMapping
    public ResponseEntity findFriendMovements(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer pagesize){
        //1.调用Service层执行业务逻辑
        PageResult<MovementsVo> pageResult = movementService.findFriendMovements(page,pagesize);

        //2.返回结果
        return ResponseEntity.ok(pageResult);
    }

//    GET /movements/recommend

    /**
     * 查询推荐好友动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer pagesize){
        //1.调用service执行业务逻辑
        PageResult pageResult = movementService.findRecommendMovements(page,pagesize);

        //2.返回结果
        return ResponseEntity.ok(pageResult);
    }

//    GET /movements/:id

    /**
     * 根据动态id查询动态详情
     * @param movementId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity findMovementByMovementId(@PathVariable("id") String movementId){
        //1.调用Service层执行业务逻辑
        MovementsVo movementsVo = movementService.findMovementByMovementId(movementId);

        //2.返回结果
        return ResponseEntity.ok(movementsVo);
    }


    /**
     * 点赞动态
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity saveLike(@PathVariable("id") String movementId){
        //1.调用Service执行业务逻辑（查询的是Comment表，则调用CommentService）
        Integer likeCount = commentService.saveLike(movementId);
        //2.返回结果
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消动态点赞
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity removeLike(@PathVariable("id") String movementId){
        //1.调用Service执行业务逻辑（查询的是Comment表，则调用CommentService）
        Integer likeCount = commentService.removeLike(movementId);
        //2.返回结果
        return ResponseEntity.ok(likeCount);
    }


    /**
     * 动态喜欢
     * @return
     * GET /movements/:id/love
     */
    @GetMapping("/{id}/love")
    public ResponseEntity saveLove(@PathVariable("id") String movementId){
        //1.调用Service执行业务逻辑（查询的是Comment表，则调用CommentService）
        Integer loveCount = commentService.saveLove(movementId);

        //2.返回查询的喜欢总数
        return ResponseEntity.ok(loveCount);
    }


    /**
     * 取消动态喜欢
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity removeLove(@PathVariable("id")String movementId){
        //1.调用Service执行业务逻辑（查询的是Comment表，则调用CommentService）
        Integer loveCount = commentService.removeLove(movementId);
        //2.返回结果
        return ResponseEntity.ok(loveCount);
    }

    /**
     * 谁看过我
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity queryVisitorsList(){
        //1.通过Service层执行业务逻辑
        List<VisitorsVo> visitorsVoList = movementService.queryVisitorsList();

        //2.返回结果
        return ResponseEntity.ok(visitorsVoList);
    }
}
